package cn.wen.forum.web;

import cn.wen.forum.common.result.JSONResult;
import cn.wen.forum.common.result.RequestParams;
import cn.wen.forum.common.result.Result;
import cn.wen.forum.config.InitSysParams;
import cn.wen.forum.pojo.entity.Post;
import cn.wen.forum.pojo.entity.Review;
import cn.wen.forum.service.IPostService;
import cn.wen.forum.service.IReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName ForumController
 * @Author By wen
 * @Description 论坛相关 主要功能 控制器
 * @Date 2023/3/7 11:09
 * @Version 1.0
 */
@Controller
@RequestMapping("/forum/*")
public class ForumController {

    @Autowired
    private IPostService iPostService;

    @Autowired
    IReviewService iReviewService;


    /**
     * 跳转详情页
     * @param id
     * @return
     */
    @GetMapping("getDetail/{id}")
    public String getDetail(@PathVariable("id")String id){
        return "detail";
    }

    /**
     * 跳转发布页面
     * @return
     */
    @GetMapping("getEdit")
    public String getEdit(){
        return "edit";
    }

    /**
     * 获取自动补全数据
     * @param key
     * @return
     */
    @GetMapping("suggestion")
    @ResponseBody
    public List<String> getSuggestion(@RequestParam("key")String key) {
        return iPostService.getSuggestion(key);
    }
    /**
     * 获取帖子集合
     * @param requestParams
     * @return
     */
    @PostMapping("postList")
    @ResponseBody
    public JSONResult searchPostList(@RequestBody RequestParams requestParams) {
        return iPostService.searchPostList(requestParams);
    }

    /**
     * 根据id获取帖子内容
     * @param id
     * @return
     */
    @GetMapping("getPost/{id}")
    @ResponseBody
    public JSONResult getPostById(@PathVariable("id")String id, HttpServletRequest request, HttpServletResponse response) {
        return iPostService.getById(id,request,response);
    }


    /**
     * 根据id获取帖子下评论内容
     * @param id
     * @return
     */
    @GetMapping("getReviewList/{id}")
    @ResponseBody
    public JSONResult getReviewList(@PathVariable("id")String id) {
        return iReviewService.getReviewListById(id);
    }

    /**
     * 提交评论
     * @return
     */
    @PostMapping("submitReview")
    @ResponseBody
    public JSONResult submitReview(@RequestBody Review review, HttpServletRequest request) throws Exception{
        return iReviewService.submitReview(review,request);
    }


    /**
     * 发布帖子
     * @return
     */
    @PostMapping("toPublish")
    @ResponseBody
    public JSONResult toPublish(@RequestBody Post post) throws Exception {
        return iPostService.toPublish(post);
    }


    /**
     * 获取等级信息展示
     * @return
     */
    @GetMapping("getLevelList")
    @ResponseBody
    public JSONResult getLevelList() {
        List levelCacheList = InitSysParams.getLevelCacheList();
        return Result.success(levelCacheList);
    }
}
